package com.github.ecbp.store.service.common;

import com.github.ecbp.common.api.IErrorCode;
import com.google.common.base.Optional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Created by Alex·He on 2020/4/28 10:54
 * @Version 1.0
 * @Description 错误码
 */
public enum StoreCodeAndMessageEnum implements IErrorCode {
    // 品牌模块错误
    BRAND_DELETE_ERROR(2000, "品牌信息删除失败"),
    BRAND_SAVE_ERROR(2001, "品牌信息更新失败"),

    // 商品分类模块错误
    PRODUCT_CATEGORY_DELETE_ERROR(2011, "商品分类信息删除失败"),
    PRODUCT_CATEGORY_SAVE_ERROR(2012, "商品分类信息更新失败"),

    // 商品属性模块错误
    PRODUCT_ATTRIBUTE_DELETE_ERROR(2021, "商品属性信息删除失败"),
    PRODUCT_ATTRIBUTE_NOT_EXISTS_ERROR(2021, "商品属性不存在"),
    PRODUCT_ATTRIBUTE_SAVE_ERROR(2023, "商品属性保存失败"),

    // 商品信息模块错误
    PRODUCT_NOT_EXISTS_ERROR(2031, "商品信息不存在"),
    PRODUCT_SAVE_ERROR(2032, "商品信息保存失败"),
    PRODUCT_CATEGORY_REL_SAVE_ERROR(2033, "商品与分类关系保存失败"),
    PRODUCT_SKU_SAVE_ERROR(2034, "商品SKU保存失败"),
    PRODUCT_PICTURE_SAVE_ERROR(2035, "商品图片保存失败"),
    PRODUCT_SPEC_SAVE_ERROR(2036, "商品规格保存失败"),
    PRODUCT_SPEC_REL_SAVE_ERROR(2037, "商品与规格关系保存失败"),
    PRODUCT_PARAM_SAVE_ERROR(2038, "商品参数保存失败"),
    PRODUCT_PARAM_REL_SAVE_ERROR(2039, "商品与参数关系保存失败"),
    PRODUCT_FULL_REDUCTION_SAVE_ERROR(2040, "商品满减信息保存失败"),
    PRODUCT_MEMBER_PRICE_SAVE_ERROR(2041, "商品会员价格信息保存失败"),
    VERTIFY_RECORD_SAVE_ERROR(2042, "商品审核记录保存失败"),
    PRODUCT_DELETE_ERROR(2043, "商品信息删除失败"),
    PRODUCT_CATEGORY_REL_DELETE_ERROR(2044, "商品与分类的关系删除失败"),
    PRODUCT_SKU_DELETE_ERROR(2045, "商品SKU删除失败"),
    PRODUCT_PICTURE_DELETE_ERROR(2046, "商品图片删除失败"),
    PRODUCT_SPEC_REL_DELETE_ERROR(2047, "商品与规格的关系删除失败"),
    PRODUCT_PARAM_REL_DELETE_ERROR(2048, "商品与参数的关系删除失败"),
    PRODUCT_FULL_REDUCTION_DELETE_ERROR(2049, "商品满减信息删除失败"),
    PRODUCT_MEMBER_PRICE_DELETE_ERROR(2050, "会员价格信息删除失败"),
    VERTIFY_RECORD_DELETE_ERROR(2051, "商品审核记录删除失败"),

    // 运费模板模块错误
    FREIGHT_TEMPLATE_DELETE_ERROR(2052, "运费模板删除失败"),

    // 优惠券模块错误
    COUPON_NOT_EXISTS_ERROR(2060, "优惠券信息不存在"),
    COUPON_SAVE_ERROR(2061, "优惠券信息保存失败"),
    COUPON_PRODUCT_CATEGORY_REL_SAVE_ERROR(2062, "优惠券与商品分类关系保存失败"),
    COUPON_PRODUCT_REL_SAVE_ERROR(2063, "优惠券与商品关系保存失败"),
    COUPON_DELETE_ERROR(2064, "优惠券信息删除失败"),
    COUPON_PRODUCT_CATEGORY_REL_DELETE_ERROR(2065, "优惠券与商品分类关系删除失败"),
    COUPON_PRODUCT_REL_DELETE_ERROR(2066, "优惠券与商品关系删除失败"),
    ;

    //code
    private Integer code;

    //描述
    private String message;

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/28 11:01
     * @version 1.0
     * @param code
     * @return 对应的枚举信息，如果code不存在，则返回null
     * @throws NullPointerException code为空则抛出该异常
     * @description 根据code获取状态值信息枚举
     */
    public static StoreCodeAndMessageEnum getByCode(Integer code) {
        Optional.of(code);
        List<StoreCodeAndMessageEnum> codeAndMessageEnums = Arrays.asList(StoreCodeAndMessageEnum.values());
        Map<Integer, StoreCodeAndMessageEnum> messageEnumMapByCode =
                codeAndMessageEnums.stream().collect(Collectors.toMap(StoreCodeAndMessageEnum::getCode, e -> e));
        return messageEnumMapByCode.get(code);
    }

    StoreCodeAndMessageEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
